package com.zksw.util;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HttpSendToll implements RequestHttp<CloseableHttpClient, HttpHost> {
    private CloseableHttpClient httpClient = DefaultApacheHttpClientBuilder.get().build();
    private HttpHost httpProxy = null;
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public CloseableHttpClient getRequestHttpClient() {
        return this.httpClient;
    }

    @Override
    public HttpHost getRequestHttpProxy() {
        return this.httpProxy;
    }

    @Override
    public HttpType getRequestType() {
        return  HttpType.APACHE_HTTP;
    }

    public String post(String url, String postData) throws WxErrorException {
        return (String)this.execute(SimplePostRequestExecutor.create(this), url, postData);
    }

    protected synchronized <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
        try {
            T result = executor.execute(uri, data);
            this.log.debug("\n【请求地址】: {}\n【请求参数】：{}\n【响应数据】：{}", new Object[]{uri, data, result});
            return result;
        } catch (WxErrorException var6) {
            WxError error = var6.getError();
            if (error.getErrorCode() != 0) {
                this.log.error("\n【请求地址】: {}\n【请求参数】：{}\n【错误信息】：{}", new Object[]{uri, data, error});
                throw new WxErrorException(error, var6);
            } else {
                return null;
            }
        } catch (IOException var7) {
            this.log.error("\n【请求地址】: {}\n【请求参数】：{}\n【异常信息】：{}", new Object[]{uri, data, var7.getMessage()});
            throw new RuntimeException(var7);
        }
    }
}
